﻿Public Class Form1

    Public CurrentWebsite As String ' stores the current website 

    ' a setting is a variable that lasts forever, they are made in the my project form in solution explorer




    Private Sub Previous_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Previous.Click
        Me.WebBrowser1.GoBack() ' tells the web browser to go back
    End Sub

    Private Sub Foward_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Foward.Click
        Me.WebBrowser1.GoForward() ' tells the web browser to go foward
    End Sub

    Private Sub Go_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Go.Click
        Me.WebBrowser1.Navigate(AddressBar.Text) ' When clicked the url in the address bar is used 
    End Sub

    Private Sub RefreshB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RefreshB.Click
        Me.WebBrowser1.Refresh() ' refreshes webpage
    End Sub

    Private Sub Home_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Home.Click

        Me.WebBrowser1.Url = New Uri(My.Settings.homeurl) ' when home is clicked the url stored in the setting home url is shown

    End Sub


    Private Sub ToolboxWeb_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim HomeURL As String = ""

        If My.Settings.FavouritesURL Is Nothing Then
            My.Settings.FavouritesURL = New System.Collections.Specialized.StringCollection
        End If

        If My.Settings.FavouriteName Is Nothing Then
            My.Settings.FavouriteName = New System.Collections.Specialized.StringCollection
        End If

        If My.Settings.homeurl = "" Then ' checks if the setting is empty
            Do Until HomeURL <> ""
                HomeURL = InputBox("You must choose a home URL.") ' uses a input box to get the desired home webpage and adds http:// to it
            Loop

            If HomeURL.Contains("http://") Then
                My.Settings.homeurl = HomeURL
            ElseIf Not HomeURL.Contains("http://") Then
                HomeURL = ("http://" & HomeURL)
            End If
            My.Settings.homeurl = HomeURL
            Options.HomeURL.Text = My.Settings.homeurl ' shows the new setting in the internet options textbox called Home URL
            AddressBar.Text = My.Settings.homeurl ' displays the contents of the setting in the address bar
        Else
            AddressBar.Text = My.Settings.homeurl ' displays the contents of the setting in the address bar
            WebBrowser1.Url = New Uri(My.Settings.homeurl) ' displays the content of the setting
        End If
    End Sub


    Private Sub Options_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles InternetOptions.Click
        Options.Show() ' shows the form called options
    End Sub

    Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        AddressBar.Text = Me.WebBrowser1.Url.ToString ' updates the addressbar with the current website URL
    End Sub

    Private Sub AddressBar_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles AddressBar.KeyDown
        If e.KeyCode = Keys.Enter Then ' when enter is pressed 
            Me.WebBrowser1.Navigate(AddressBar.Text) ' When clicked the url in the address bar is used 
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Input_Favourite.Show()
    End Sub

    Private Sub Minimise_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Minimise.Click
        favourites.Visible = False ' minimises the favourites
        WebBrowser1.Height = (704) ' changes height back to normal
        WebBrowser1.Location = New Point(0, 36)
    End Sub

    Private Sub FavouritesTab_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FavouritesTab.Click
      

        WebBrowser1.Height = (593)          ' changes the height of the browser to compensate for the favourite panel
        WebBrowser1.Location = New Point(0, 170)    ' changes the location of the browser so its not squashed
        favourites.Visible = True               ' shows the panel       
        
        FavouriteShow()

    End Sub

    Sub FavouriteShow()

        Dim labelX1 As Integer = 136
        Dim labelY1 As Integer = 12         ' X1 and Y1 are both first row 
        Dim labelX2 As Integer = 136
        Dim labelY2 As Integer = 37         ' X2 and Y2 are both second Row
        Dim tagnumber As Integer = 0


        ' need to remove old labels for this to work
        For i As Integer = 0 To Tabpage1.Controls.Count - 1
            If Tabpage1.Controls.Item(i).Name = "Favourite" Then
                Tabpage1.Controls.Item(i).Dispose()
                Tabpage1.Update()
                If Tabpage1.Controls.Count = i Then
                    Exit For
                Else
                    i -= 1
                End If

            End If
        Next

        For i As Integer = 0 To My.Settings.FavouriteName.Count - 1 ' creates a loop that processes the amount of times that favourite has values
            Dim Favourite As New Label ' creates a label called Favourite
            Favourite.Name = "Favourite"
            If My.Settings.FavouriteName.Count <> 0 Then ' Checks if it has any values
                Favourite.Parent = Tabpage1 ' shows it where to appear
                Favourite.Text = My.Settings.FavouriteName.Item(i) ' gives it the name of the favourite Name
                Favourite.Width = GetWidth(Favourite.Text, Favourite.Font) + 5 ' makes it the correct width
                If labelX1 <= 980 And labelY1 = 12 Then ' checks if it has reached the end of the column
                    Favourite.Location = New Point(labelX1, labelY1)    ' give the location of the label    
                    labelX1 = labelX1 + Favourite.Width ' applies the width to the length to give even spacing
                ElseIf labelX1 >= 950 And labelY1 = 12 Then ' checks if it is at the edge of the area
                    Favourite.Location = New Point(labelX2, labelY2) ' sets the location of the label
                    labelX2 = labelX2 + Favourite.Width ' applies the width to the length to give even spacing

                End If
            End If

            Dim ContextMenu As New ContextMenuStrip ' creates a new context menu strip

            ContextMenu.Items.Add("Delete") ' adds the item called Delete
            ContextMenu.Tag = Favourite ' shows it where to appear

            AddHandler ContextMenu.Items(0).Click, AddressOf DeleteFavouriteToolStripMenuItem_Click ' creates the even to check if it has been clicked
            Favourite.ContextMenuStrip = ContextMenu ' shows it where to appear
            Favourite.Tag = tagnumber   ' gives it a tag number
            tagnumber = tagnumber + 1 ' increments the tag
            AddHandler Favourite.MouseClick, AddressOf Favourite_Click ' creates a event if the favourite has been clicked
        Next
    End Sub

    Private Sub Favourite_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        WebBrowser1.Url = New Uri(My.Settings.FavouritesURL.Item(sender.tag)) ' follows the URL that the favourite has
    End Sub

    Function GetWidth(ByVal str As String, ByVal fnt As Font) As Integer            ' gets the width of the label
        Using img As New Bitmap(1, 1), g As Graphics = Graphics.FromImage(img)
            Return g.MeasureString(str, fnt).Width
        End Using
    End Function

    Private Sub DeleteFavouriteToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles DeleteFavouriteToolStripMenuItem.Click
        sender.owner.tag.dispose() '  removes the label when deleted
        sender.owner.tag.update()   ' updates it to show removal
        My.Settings.FavouriteName.RemoveAt(sender.owner.tag.tag) ' removes the name of the favourite
        My.Settings.FavouritesURL.RemoveAt(sender.owner.tag.tag)    ' roves the url of the favourite

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        My.Settings.FavouriteName.Clear()
        My.Settings.FavouritesURL.Clear()
        My.Settings.Save()
    End Sub

    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        My.Settings.FavouriteName.Clear()
        My.Settings.FavouritesURL.Clear()
    End Sub
End Class
